/**
 *
 */
package com.angel.data.generator.repository;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;

import com.angel.common.helpers.AnnotationHelper;
import com.angel.common.helpers.ReflectionHelper;
import com.angel.data.generator.annotations.importFileProcessorRunner.ImportFileProcessorRunnerBuilder;
import com.angel.data.generator.annotations.inputStream.InputStreamBuilder;
import com.angel.data.generator.exceptions.DataGeneratorException;
import com.angel.data.generator.interfaces.DataGenerator;
import com.angel.data.generator.interfaces.impl.AnnotationDataGenerator;

/**
 * @author William
 *
 */
public class AnnotationDataGeneratorRepository extends DataGeneratorRepository{

	private static Logger LOGGER = Logger.getLogger(AnnotationDataGeneratorRepository.class);

	public AnnotationDataGeneratorRepository(){
		super();
	}

	@Override
	protected DataGenerator createDataGeneratorFor(Class<?> dataGeneratorClass){
		LOGGER.debug("Creating a data class generator instance for [" + dataGeneratorClass.getCanonicalName() + "].");
		Object dataGeneratorObject = ReflectionHelper.createObject(dataGeneratorClass);
		Method imporFileProcessorRunnerBuilderMethod = AnnotationHelper.getMethodWithAnnotation(dataGeneratorClass, ImportFileProcessorRunnerBuilder.class);
		Method inputStreamBuilderMethod = AnnotationHelper.getMethodWithAnnotation(dataGeneratorClass, InputStreamBuilder.class);
		if(imporFileProcessorRunnerBuilderMethod == null){
			LOGGER.error("Data generator class [" + dataGeneratorClass.getCanonicalName() + "] must have a method with ImportFileProcessorRunnerBuilder annotation.");
			throw new DataGeneratorException("Data generator class [" + dataGeneratorClass.getCanonicalName() + "] must have a method with ImportFileProcessorRunnerBuilder annotation.");
		}
		if(inputStreamBuilderMethod == null){
			LOGGER.error("Data generator class [" + dataGeneratorClass.getCanonicalName() + "] must have a method with InputStreamBuilder annotation.");
			throw new DataGeneratorException("Data generator class [" + dataGeneratorClass.getCanonicalName() + "] must have a method with InputStreamBuilder annotation.");
		}
		DataGenerator dataGenerator = (DataGenerator) new AnnotationDataGenerator(dataGeneratorObject, imporFileProcessorRunnerBuilderMethod, inputStreamBuilderMethod);
		return dataGenerator;
	}
}
